<?php
function to_utf($str) { return iconv("windows-1251", "UTF-8", $str); }


function eflush() {
	//	ob_flush();
	flush();
}


function search_infile($filename, $str_in_row, $str1, $str2) {
	$fd = fopen($filename, "r");
	$bufer = fread($fd, filesize($filename));
	fclose($fd);
	$bufer = split("\n", $bufer);
	for($i=0; $i<count($bufer); $i++) {
		$tmp = strpos($bufer[$i], $str_in_row, 0);
		if($tmp !== false) {
			$line = $bufer[$i];
			if($str1) { $line = substr($line, strrpos($line, $str1, 0) + strlen($str1)); }
			if($str2) {	$line = substr($line, 0, strpos($line, $str2));	}
			$line_arr[] = $line; // ne vse tut horosho
		}
	}
	if(isset($line_arr)) { return $line_arr; }
}


function get_cookies($lcm, $pcm) {
	global $obj_curl, $temp_dir;
	if(!is_dir($temp_dir."/".$lcm)) { mkdir($temp_dir."/".$lcm); }
	$obj_curl->url = "http://vkontakte.ru/login.php";
	$obj_curl->filename = $temp_dir."/".$lcm."/login.html";
	$obj_curl->post = "email=".$lcm."&pass=".$pcm;
	$obj_curl->method_main_curl();
	$tmp_arr = search_infile($obj_curl->cookie_file, "remixemail", "remixemail\t", "");
	$cookie_login = $tmp_arr[0];
	echo $cookie_login."\n"; eflush();
}


include_once("class_main_curl.php");
$temp_dir="temp-fr";
$obj_curl = new main_curl();
$obj_curl->cookie_file = $temp_dir."/cookie.txt";
get_cookies("michael.kotov.melbourne@gmail.com", "calipso09");
$st=400;
$dbcnx = @mysql_connect("localhost", "root", "qqq");
mysql_query("SET NAMES utf8");
@mysql_select_db("vksearchdb", $dbcnx);

do {
	echo "st=".$st."\n";
	$obj_curl->url = "http://vkontakte.ru/groups_ajax.php";
	$obj_curl->post = "act=getpages&gid=6352105&offset=".$st."&op=2";
	$obj_curl->filename = $temp_dir."/fsearch-".$st.".html";
	$obj_curl->method_main_curl();
	usleep(1100000);
	unset($group_friends_arr);
	$group_friends_arr = search_infile($obj_curl->filename, "memberOptions_", "memberOptions_", "\"");
	for($i=0; $i<count($group_friends_arr); $i++) {
		unset($ids); unset($flags);
		$id_result = mysql_query("SELECT id_vk,flag FROM table_main WHERE id_vk=".$group_friends_arr[$i]);
		$id_counter=0;
		while($id_row = mysql_fetch_row($id_result)) {
			list($id_vk,$flag) = $id_row;
			$ids[$id_counter] = $id_vk;
			$flags[$id_counter] = $flag;
			$id_counter++; // количество id вконтакте
		}
		if(isset($ids)) {
		 	if($flags[0] != 7) {
				echo "[".($st+$i)."] [".$group_friends_arr[$i]."] flag=".$flags[0];
		 		echo " set dbflag to 7";
		 		mysql_query("UPDATE table_main SET flag=7 WHERE id_vk=".$group_friends_arr[$i]);
		 		echo "\n";
		 	}
		} else {
			echo "[".($st+$i)."] [".$group_friends_arr[$i]."] need to add!!!\n";
		}
	}
	$st+=50;
} while ($st<=950);
mysql_close($dbcnx);
?>
